package com.zmj.sy.mom.srv.aps.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmj.sy.mom.srv.aps.bean.entity.resource.PlateStandard;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BaseIdReqVo;
import com.zmj.sy.mom.srv.aps.bean.vo.base.BasePageResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.plate.*;
import com.zmj.sy.mom.srv.aps.mapper.PlateStandardMapper;
import com.zmj.sy.mom.srv.aps.utils.MkBeanUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

@RequiredArgsConstructor
@Service
public class PlateStandardService {

    private final PlateStandardMapper plateStandardMapper;

    public BasePageResVo<PlateStandardResVo> plateList(PlateStandardReqVo reqVo) {
        LambdaQueryWrapper<PlateStandard> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(reqVo.getHeight() != null, PlateStandard::getHeight, reqVo.getHeight());
        queryWrapper.like(StringUtils.hasText(reqVo.getTexture()), PlateStandard::getTexture, reqVo.getTexture());
        queryWrapper.orderByDesc(PlateStandard::getId);
        Page<PlateStandard> page = plateStandardMapper.selectPage(reqVo.toPage(), queryWrapper);

        for (PlateStandard r : page.getRecords()) {
            r.setPlateRate(r.getPlateRate().setScale(2, RoundingMode.HALF_UP));
        }

        return BasePageResVo.of(page, PlateStandardResVo.class);
    }

    @Transactional
    public void plateAdd(PlateStandardAddReqVo reqVo) {
        PlateStandard plateStandard = MkBeanUtils.copyProperties(reqVo, PlateStandard.class);
        plateStandard.setNorm(plateStandard.getTexture() + "δ" + plateStandard.getHeight().toString());
        List<PlateStandard> list = plateStandardMapper.lambdaQuery().eq(PlateStandard::getHeight, reqVo.getHeight()).eq(PlateStandard::getTexture, reqVo.getTexture()).list();
        if (list.isEmpty()) {
            plateStandardMapper.insert(plateStandard);
        }
    }

    @Transactional
    public void plateEdit(PlateStandardEditReqVo reqVo) {
        PlateStandard plateStandard = MkBeanUtils.copyProperties(reqVo, PlateStandard.class);
        plateStandard.setNorm(plateStandard.getTexture() + "δ" + plateStandard.getHeight().toString());
        plateStandardMapper.updateById(plateStandard);
    }

    @Transactional
    public void plateDelete(BaseIdReqVo reqVo) {
        plateStandardMapper.deleteById(reqVo.getId());
    }

    @Transactional
    public void autoCreatePlate(PlateStandardCreateResVo reqVo) {
        List<String> texture = reqVo.getTexture();
        List<Integer> thickness = reqVo.getThickness();
        for (String s : texture) {
            for (Integer i : thickness) {
                PlateStandardAddReqVo vo = new PlateStandardAddReqVo();
                vo.setTexture(s);
                vo.setHeight(i);
                vo.setLength(10000);
                vo.setWidth(2500);
                vo.setPlateRate(BigDecimal.valueOf(85));
                plateAdd(vo);
            }
        }
        // texture:  "Q355B","HS90","Q235","Q460","Q460_NS","QQ460MC","Q460W","Q550","Q690","Q460Q","Q550Q","Q690Q","Q890Q"
        // thickness:  4,12,16,20,25,30,40,50,55,60,80
    }

}
